package com.cloud.form.interfaces;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.cloud.form.common.config.FeignConfiguration;
import com.cloud.form.common.result.SelectItem;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import feign.hystrix.FallbackFactory;

@FeignClient(value = "SYSTEM", configuration = FeignConfiguration.class, fallbackFactory = FeignClientSystemFallbackFactory.class)
public interface FeignClientSystem {
	/**
	 * 获取字典项
	 * 
	 * @param sysId
	 *            子系统编码
	 * @param dictKey
	 *            字典键值
	 * @return
	 */
	@RequestMapping(value = "system/interface/sysdict/listdicts", method = RequestMethod.GET)
	public List<SelectItem> loadDictItems(@RequestParam("dictKey") String dictKey);

	/**
	 * 获取当前登录人SQL解析字段相关信息
	 * 
	 * @param humanId
	 * @return
	 */
	@RequestMapping(value = "system/interface/humanunit/info", method = RequestMethod.GET)
	public Map<String, String> getSystemParams(@RequestParam("humanId") long humanId);

	/**
	 * 获取地址信息
	 *
	 * @return
	 */
	@RequestMapping(value = "system/interface/address/load", method = RequestMethod.GET)
	public List<SelectItem> loadAddressItems();

	@RequestMapping(value = "system/interface/sysconfig/item", method = RequestMethod.GET)
	String getSysConfigItem(@RequestParam("configKey") String configName);

	/**
	 * 获取字典项
	 * 
	 * @param dictKeys
	 *            字典键值
	 * @return
	 */
	@RequestMapping(value = "system/interface/sysdict/dicts")
	public Map<String, List<SelectItem>> loadDictItems(@RequestBody List<String> dictKeys);

}

@Component
class FeignClientSystemFallbackFactory implements FallbackFactory<FeignClientSystem> {
	private static final Logger LOGGER = LoggerFactory.getLogger(FeignClientSystemFallbackFactory.class);

	@Override
	public FeignClientSystem create(final Throwable cause) {
		return new FeignClientSystem() {

			@Override
			public List<SelectItem> loadDictItems(String dictKey) {
				if (FeignClientSystemFallbackFactory.LOGGER.isDebugEnabled()) {
					FeignClientSystemFallbackFactory.LOGGER.debug("loadDictItems fallback; reason was:" + cause);
				}
				return Collections.emptyList();
			}

			@SuppressWarnings("unchecked")
			@Override
			public Map<String, String> getSystemParams(long humanId) {
				if (FeignClientSystemFallbackFactory.LOGGER.isDebugEnabled()) {
					FeignClientSystemFallbackFactory.LOGGER.debug("loadDictItems fallback; reason was:" + cause);
				}
				return MapUtils.EMPTY_MAP;
			}

			@Override
			public List<SelectItem> loadAddressItems() {
				if (FeignClientSystemFallbackFactory.LOGGER.isDebugEnabled()) {
					FeignClientSystemFallbackFactory.LOGGER.debug("loadAddressItems fallback; reason was:" + cause);
				}
				return Collections.emptyList();
			}

			@Override
			public String getSysConfigItem(String configName) {
				if (FeignClientSystemFallbackFactory.LOGGER.isDebugEnabled()) {
					FeignClientSystemFallbackFactory.LOGGER.debug("getMediaRootPath fallback; reason was:" + cause);
				}
				return null;
			}

			@Override
			public Map<String, List<SelectItem>> loadDictItems(List<String> dictKeys) {
				if (FeignClientSystemFallbackFactory.LOGGER.isDebugEnabled()) {
					FeignClientSystemFallbackFactory.LOGGER.debug("loadDictItems fallback; reason was:" + cause);
				}
				return null;
			}

		};
	}

}
